/* Copyright (c) 2002-2009 Grupo e-Gen.  All rights reserved.
 *
 * ====================================================================
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * either version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 */
 
package br.com.managerprojectplugin.view;

import java.util.ArrayList;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;

import br.com.managerprojectplugin.model.Stakeholder;

/**
 * Class: StakeHoldersview
 *   
 * @author Grupo e-Gen 
 * @version 1.0 (07/07/2012)
 * @since 1.0 (14/07/2012)
 */
public class StakeHoldersview extends ViewPart {

	public static final String STAKEHOLDERS_ID = "StakeHoldersView";
	private TableViewer tableViewer;
	private ArrayList<Stakeholder>stkhldrs = null;
	public StakeHoldersview() {
	}
	

	@Override
	public void createPartControl(Composite parent) {
		//Inicializando as variaveis
		stkhldrs = new ArrayList<Stakeholder>();
		for (int i = 0; i<5; i++) {
			stkhldrs.add(new Stakeholder());
			stkhldrs.get(i).setNome("Nome "+i);
			stkhldrs.get(i).setTelefone("8388380"+i);
			stkhldrs.get(i).setEmail("carolinaaa"+i+"@bol.com");
		}
		
		//Criando a table
		tableViewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		
		//Criando as colunas
		TableViewerColumn skNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		skNameColumn.setEditingSupport(new EditingSupport(skNameColumn.getViewer()) {
			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Stakeholder){
					Stakeholder stakeholder = (Stakeholder) element;
					stakeholder.setNome((String) value);
					tableViewer.refresh(stakeholder);
				}
			}
			
			@Override
			protected Object getValue(Object element) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					return stakeholder.getNome();
				}
				return null;
			}
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Stakeholder)
					return new TextCellEditor(tableViewer.getTable());
				return null;
			}
			
			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});
		
		TableViewerColumn skPhoneColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		skPhoneColumn.setEditingSupport(new EditingSupport(skPhoneColumn.getViewer()) {
			
			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Stakeholder){
					Stakeholder stakeholder = (Stakeholder) element;
					stakeholder.setTelefone((String)value);
					tableViewer.refresh(stakeholder);
				}
			}
			
			@Override
			protected Object getValue(Object element) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					return stakeholder.getTelefone();
				}
				return null;
			}
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Stakeholder){
					return new TextCellEditor(tableViewer.getTable());
				}
				return null;
			}
			
			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});

		TableViewerColumn skMailColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		skMailColumn.setEditingSupport(new EditingSupport(skMailColumn.getViewer()) {
			
			@Override
			protected void setValue(Object element, Object value) {
				if (element instanceof Stakeholder){
					Stakeholder stakeholder = (Stakeholder) element;
					stakeholder.setEmail((String)value);
					tableViewer.refresh(stakeholder);
				}
			}
			
			@Override
			protected Object getValue(Object element) {
				if (element instanceof Stakeholder) {
					Stakeholder stakeholder = (Stakeholder) element;
					return stakeholder.getEmail();
				}
				return null;
			}
			
			@Override
			protected CellEditor getCellEditor(Object element) {
				if (element instanceof Stakeholder){
					return new TextCellEditor(tableViewer.getTable());
				}
				return null;
			}
			
			@Override
			protected boolean canEdit(Object element) {
				return true;
			}
		});
		
		//tornando a tabela visivel
		tableViewer.getTable().setHeaderVisible(true);
		tableViewer.setContentProvider(new StakeHoldersContentProvider());
		tableViewer.setLabelProvider(new StakeHoldersLabelProvider());
		
		GridData gd = new GridData();
		gd.horizontalAlignment = SWT.FILL;
		gd.verticalAlignment = SWT.FILL;
		gd.grabExcessVerticalSpace = true;
		gd.grabExcessHorizontalSpace = true;
		
		tableViewer.setInput(stkhldrs);
		tableViewer.getTable().setLayoutData(gd);
		
		//dimensionar
		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(30, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(30, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100,true));
		tableLayout.addColumnData(new ColumnWeightData(20, 100,true));
		tableViewer.getTable().setLayout(tableLayout);
		
		Composite composite = new Composite(parent, SWT.NONE);
		Button addSk = new Button(composite, SWT.PUSH);
		addSk.setText("Add");
		addSk.setSize(50, 50);
		
		addSk.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				// TODO Auto-generated method stub
				super.widgetSelected(e);
				stkhldrs.add(new Stakeholder("Digite o nome", "digite o email", "digite o telefone"));
				tableViewer.refresh();
			}
		});
		
		PlatformUI.getWorkbench().getHelpSystem().setHelp(tableViewer.getControl(), "Manager-projects-plugin");
	}
	
	
	
	@Override
	public void setFocus() {
		tableViewer.getControl().setFocus();
	}
	

}
